﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using M3G;

namespace MyTestProject {

    public class TestRenderPass {
        [Fact]
        public void TestConstructor () {
            var pass = new RenderPass ();

            Assert.Equal (null, pass.World);
            Assert.Equal (null, pass.Target);
            Assert.Equal (null, pass.Camera);
            Assert.Equal (null, pass.Background);
            Assert.Equal (0.0f, pass.DepthRangeNear);
            Assert.Equal (1.0f, pass.DepthRangeFar);
        }

        [Fact]
        public void TestSetWorld () {
            var pass = new RenderPass ();
            var wld = new World ();
            pass.SetWorld (wld);

            Assert.Equal (wld, pass.World);
        }

        [Fact]
        public void TestSetBackground () {
            var pass = new RenderPass ();
            var bg = new Background ();
            pass.SetBackground (bg);

            Assert.Equal (bg, pass.Background);
        }

        [Fact]
        public void TestSetCamera () {
            var pass = new RenderPass ();
            var cam = new Camera ();
            pass.SetCamera (cam);

            Assert.Equal (cam, pass.Camera);
        }

        [Fact]
        public void TestSetViewport () {
            var pass = new RenderPass ();
            pass.SetViewport (1, 2, 640, 480);

            Assert.Equal (1, pass.ViewportX);
            Assert.Equal (2, pass.ViewportY);
            Assert.Equal (640, pass.ViewportWidth);
            Assert.Equal (480, pass.ViewportHeight);
        }

        [Fact]
        public void TestDepthRange () {
            var pass = new RenderPass ();
            pass.SetDepthRange (0.1f, 0.2f);

            Assert.Equal (0.1f, pass.DepthRangeNear);
            Assert.Equal (0.2f, pass.DepthRangeFar);
        }


        [Fact]
        public void TestSetTargetImage2D () {
            var pass = new RenderPass ();
            var img = new Image2D (Image2D.RGB, 16, 16);
            var target = new RenderTarget (img, 0);
            var flags = Graphics3D.Antialias | Graphics3D.Depth | Graphics3D.Dither;

            pass.SetTarget (target, flags);

            Assert.Equal (target, pass.Target);
            Assert.Equal (flags, pass.TargetFlags);
        }

        [Fact (Skip = "未実装")]
        public void TestSetTargetImageCube () {

            // 未実装
        }

        [Fact]
        public void TestReferences () {
            var pass = new RenderPass ();
            var wld  = new World ();
            var bg   = new Background ();
            var cam  = new Camera ();
            pass.SetWorld (wld);
            pass.SetBackground (bg);
            pass.SetCamera (cam);

            var refs = pass.References;

            Assert.Equal (3, refs.Length);
            Assert.Same (wld, refs[0]);
            Assert.Same (cam, refs[1]);
            Assert.Same (bg, refs[2]);
        }

        [Fact]
        public void TestDuplicate () {
            var pass = new RenderPass ();
            var wld  = new World ();
            var bg   = new Background ();
            var cam  = new Camera ();
            var img = new Image2D (Image2D.RGB, 16, 16);
            var target = new RenderTarget (img, 0);
            var flags = Graphics3D.Antialias | Graphics3D.Depth | Graphics3D.Dither;

            pass.SetWorld (wld);
            pass.SetBackground (bg);
            pass.SetCamera (cam);
            pass.SetTarget (target, flags);
            pass.SetDepthRange (0.1f, 0.2f);
            pass.SetViewport (1, 2, 640, 480);
            pass.UserID = 100;

            var dup = (RenderPass)pass.Duplicate ();

            Assert.Equal (pass.World, dup.World);
            Assert.Equal (pass.Target, dup.Target);
            Assert.Equal (pass.Camera, dup.Camera);
            Assert.Equal (pass.Background, dup.Background);
            Assert.Equal (pass.DepthRangeNear, dup.DepthRangeNear);
            Assert.Equal (pass.DepthRangeFar, dup.DepthRangeFar);
            Assert.Equal (pass.UserID, dup.UserID);
        }
    }
}
